package service.impl;

import dao.AddressDao;
import dao.GoodsDao;
import dao.OrdersDao;
import dao.UserDao;
import dao.impl.AddressDaoImpl;
import dao.impl.GoodsDaoImpl;
import dao.impl.OrdersDaoImpl;
import dao.impl.UserDaoImpl;
import model.Address;
import model.Goods;
import model.Orders;
import model.User;
import service.OrdersService;
import vo.OrderVo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class OrderServiceImpl implements OrdersService {
    private OrdersDao orderdao = new OrdersDaoImpl();
    private UserDao userDao = new UserDaoImpl();
    private GoodsDao goodsDao = new GoodsDaoImpl();
    private AddressDao addressDao = new AddressDaoImpl();
    @Override
    public boolean add(Orders order) { return orderdao.add(order); }

    @Override
    public List<Orders> findAll() {
        return orderdao.findAll();
    }

    @Override
    public Orders findByID(Integer id) {
        return orderdao.findByPK(id);
    }

    @Override
    public boolean delById(Integer id) { return orderdao.delByPK(id); }

    @Override
    public boolean update(Orders order) { return orderdao.update(order); }

    @Override
    public List<Orders> findByUserId(int id) {
        List<Orders> orders = orderdao.findByUserId(id,-1);
        User user = userDao.findUserByUserId(id);
        //List的遍历修改！！
        ListIterator<Orders> it =orders.listIterator();
        while(it.hasNext()){
            Orders order = it.next();
            order.setUser(user);
            Address address = addressDao.findByPK(order.getAddress().getId());
            order.setAddress(address);
            Goods goods = goodsDao.findByPK(order.getGoods().getId());
            order.setGoods(goods);
            it.set(order);
        }
        return orders;
    }

    @Override
    public List<OrderVo> findOrderInfo(int id,int state) {
        List<Orders> orders = orderdao.findByUserId(id,state);
        List<OrderVo> orderVos = new ArrayList<>();
        User user = userDao.findUserByUserId(id);
        //List的遍历修改！！
        ListIterator<Orders> it =orders.listIterator();
        while(it.hasNext()){
            Orders order = it.next();
            order.setUser(user);
            Address address = addressDao.findByPK(order.getAddress().getId());
            order.setAddress(address);
            Goods goods = goodsDao.findByPK(order.getGoods().getId());
            order.setGoods(goods);
            it.set(order);
            BigDecimal number = new BigDecimal(order.getNumber());
            OrderVo orderVo = new OrderVo(String.valueOf(order.getId()),order.getOrder_no(),goods.getName(),address.getPhone(),address.getName(),address.getShipping(),goods.getDescription(),order.getReal_price().multiply(number).toString(),String.valueOf(order.getState()));
            orderVos.add(orderVo);
        }
        return orderVos;
    }

    @Override
    public OrderVo findOrderVoById(int id) {
        Orders order = orderdao.findByPK(id);
        Address address = addressDao.findByPK(order.getAddress().getId());
        order.setAddress(address);
        Goods goods = goodsDao.findByPK(order.getGoods().getId());
        order.setGoods(goods);
        BigDecimal number = new BigDecimal(order.getNumber());
        OrderVo orderVo = new OrderVo(String.valueOf(order.getId()),order.getOrder_no(),goods.getName(),address.getPhone(),address.getName(),address.getShipping(),goods.getDescription(),order.getReal_price().multiply(number).toString(),String.valueOf(order.getState()));
        return orderVo;
    }

    @Override
    public Orders findOrderByOrderNo(String order_no) {
        return orderdao.findOrderByOrderNo(order_no);
    }

    @Override
    public OrderVo findOrderVoByOrderNo(String order_no) {
        Orders order = orderdao.findOrderByOrderNo(order_no);
        Address address = addressDao.findByPK(order.getAddress().getId());
        order.setAddress(address);
        Goods goods = goodsDao.findByPK(order.getGoods().getId());
        order.setGoods(goods);
        BigDecimal number = new BigDecimal(order.getNumber());
        OrderVo orderVo = new OrderVo(String.valueOf(order.getId()),order.getOrder_no(),goods.getName(),address.getPhone(),address.getName(),address.getShipping(),goods.getDescription(),order.getReal_price().multiply(number).toString(),String.valueOf(order.getState()));
        return orderVo;
    }

    @Override
    public boolean updateOrderState(String order_no, int state) {
        return orderdao.updateOrderStateById(order_no,state);
    }

    @Override
    public List<OrderVo> findAll(int page, int limit) {
        List<Orders> orders = orderdao.findAll(page,limit);
        List<OrderVo> orderVos = new ArrayList<>();
        //List的遍历修改！！
        ListIterator<Orders> it =orders.listIterator();
        while(it.hasNext()){
            Orders order = it.next();
            User user = userDao.findUserByUserId(order.getUser().getId());
            Address address = addressDao.findByPK(order.getAddress().getId());
            order.setAddress(address);
            Goods goods = goodsDao.findByPK(order.getGoods().getId());
            order.setGoods(goods);
            it.set(order);
            BigDecimal number = new BigDecimal(order.getNumber());
            OrderVo orderVo = new OrderVo(String.valueOf(order.getId()),order.getOrder_no(),goods.getName(),address.getPhone(),address.getName(),address.getShipping(),goods.getDescription(),order.getReal_price().multiply(number).toString(),String.valueOf(order.getState()));
            orderVos.add(orderVo);
        }
        return orderVos;
    }


    @Override
    public int findCount() {
        return orderdao.findCount();
    }


    @Override
    public int findCount(int state) {
        return orderdao.findCount(state);
    }

    @Override
    public List<OrderVo> findAllByState(int state, int page, int limit) {
        List<Orders> orders = orderdao.findAllByState(state,page,limit);
        List<OrderVo> orderVos = new ArrayList<>();
        //List的遍历修改！！
        ListIterator<Orders> it =orders.listIterator();
        while(it.hasNext()){
            Orders order = it.next();
            User user = userDao.findUserByUserId(order.getUser().getId());
            Address address = addressDao.findByPK(order.getAddress().getId());
            order.setAddress(address);
            Goods goods = goodsDao.findByPK(order.getGoods().getId());
            order.setGoods(goods);
            it.set(order);
            BigDecimal number = new BigDecimal(order.getNumber());
            OrderVo orderVo = new OrderVo(String.valueOf(order.getId()),order.getOrder_no(),goods.getName(),address.getPhone(),address.getName(),address.getShipping(),goods.getDescription(),order.getReal_price().multiply(number).toString(),String.valueOf(order.getState()));
            orderVos.add(orderVo);
        }
        return orderVos;

    }


}
